//
//  TCM_HomeTitleCycleView.swift
//  TCM_TradingProject
//
//  Created by 吕春海 on 2018/8/21.
//  Copyright © 2018年 lvchunhai. All rights reserved.
//

import UIKit

class TCM_HomeTitleCycleView: UIView {

    //要显示几行title
    var titleCount:NSInteger? {
        didSet {
            guard let titleCount = titleCount else {
                return
            }
            let titleHeight: CGFloat = self.frame.size.height/CGFloat(titleCount)
            for i in 0...titleCount - 1 {
                let titlelabel = UILabel()
                titlelabel.tag = 1000 + i
                titlelabel.textColor = TCM_HexRGB(0x5C5C5C)
                titlelabel.font(14)
                titlelabel.frame = CGRect(x: 0, y: titleHeight * CGFloat(i), width: self.frame.size.width, height: titleHeight)
                self.addSubview(titlelabel)
            }
        }
    }

    //多行显示数据
    var titleArray:[String]? {
        didSet {
            addTitleWithCount(titleArray ?? [])
        }
    }
    
    //单行显示数据
    var singleData: String? {
        didSet {
            addTitleWithCount([singleData ?? ""])
        }
    }
    
    //图片
    var imageName: String? {
        didSet {
            guard let imageName = imageName else { return }
            let imageView = self.viewWithTag(100) as! UIImageView
            imageView.image = UIImage(named: imageName)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

// MARK: -添加title
extension TCM_HomeTitleCycleView {
    
    fileprivate func addTitleWithCount(_ titleArray: [String]) {
        let count = titleArray.count
        guard count != 0 else {
            return
        }
        
        for i in 0...count-1 {
            let title = self.viewWithTag(1000+i) as! UILabel
            title.text = titleArray[i]
        }
        
    }
}
